package com.shenfc.fxzj.util;

import com.shenfc.fxzj.domain.MenuJson;
import com.shenfc.fxzj.domain.SysMenu;
import com.shenfc.fxzj.domain.SysMenuBtn;
import com.shenfc.fxzj.domain.TreeNode;

import java.util.ArrayList;
import java.util.List;


public class TreeUtil {
	private final static String MENU_ID = "menu_";

	private final static String BTN_ID = "btn_";

	List<SysMenu> menus;

	List<SysMenu> rootMenus;
	List<SysMenu> childMenus;
	List<SysMenuBtn> childBtns;

	public TreeUtil(List<SysMenu> menus) {
		this.menus = menus;
	}

	public TreeUtil(List<SysMenu> menus, List<SysMenuBtn> childBtns) {
		this.childBtns = childBtns;
		this.menus = menus;
	}

	public TreeUtil(List<SysMenu> rootMenus, List<SysMenu> childMenus,
			List<SysMenuBtn> childBtns) {
		this.rootMenus = rootMenus;
		this.childMenus = childMenus;
		this.childBtns = childBtns;
	}

	private TreeNode menuToNode(SysMenu menu) {
		if (menu == null) {
			return null;
		}
		TreeNode node = new TreeNode();
		node.setId(MENU_ID + menu.getId());
		node.setDataId(menu.getId());
		node.setText(menu.getMenuName());
		node.setParentId(menu.getParentId());
		node.setMenuIcon(menu.getMenuIcon());
		node.setUrl(menu.getMenuUrl());
		node.getAttributes().put("type", "0");
		node.getAttributes().put("id", menu.getId());
		node.getAttributes().put("url", menu.getMenuUrl());
//		if("0".equals(menu.getEnd())){
//			node.setState("closed");
//		}
		return node;
	}

	private MenuJson menuToNode2(SysMenu menu) {
		if (menu == null) {
			return null;
		}
		MenuJson menuJson = new MenuJson();
		menuJson.setId(menu.getId());
		menuJson.setTitle(menu.getMenuName());
		menuJson.setIcon(menu.getMenuIcon());
		menuJson.setHref(menu.getMenuUrl());
		return menuJson;
	}

	/**

	 * @return
	 */
	private TreeNode BtnToNode(SysMenuBtn btn) {
		if (btn == null) {
			return null;
		}
		TreeNode node = new TreeNode();
		node.setId(BTN_ID + btn.getId());
		node.setDataId(btn.getId());
		node.setText(btn.getBtnName());
		node.setParentId(btn.getMenuId());
		node.getAttributes().put("type", "1");
		node.getAttributes().put("id", btn.getId());
		return node;
	}

	public List<MenuJson> getLayUiMenus() {
		List<MenuJson> rootNodes = new ArrayList<MenuJson>();
		for (SysMenu menu : menus) {
			if (0L == menu.getParentId()) {
				MenuJson node = menuToNode2(menu);
				if (node != null) {
					addChlidNodes2(node);
					rootNodes.add(node);
				}
			}
		}
		return rootNodes;
	}

	public List<TreeNode> getRootNodes() {
		List<TreeNode> rootNodes = new ArrayList<TreeNode>();
		for (SysMenu menu : menus) {
			if (0L == menu.getParentId()) {
				TreeNode node = menuToNode(menu);
				if (node != null) {
					addChlidNodes(node);
					rootNodes.add(node);
				}
			}
		}
		return rootNodes;
	}
//hxb

	/**
	 *
	 * @return
	 */
	private void addChlidNodes(TreeNode rootNode) {
		List<TreeNode> childNodes = new ArrayList<TreeNode>();
		for (SysMenu menu : menus) {
			if (rootNode.getDataId().equals(menu.getParentId())) {
				TreeNode node = menuToNode(menu);
				if (childBtns != null && !childBtns.isEmpty()) {
					addChlidBtn(node);
				}
				if (!"1".equals(menu.getEnd())) {
					addChlidNodes(node);
				}

				childNodes.add(node);
			}
		}
		rootNode.setChildren(childNodes);
	}

	private void addChlidNodes2(MenuJson menuJson) {
		List<MenuJson> childMenuJsons = new ArrayList<MenuJson>();
		for (SysMenu menu : menus) {
			if (menuJson.getId().equals(menu.getParentId())) {
				MenuJson node = menuToNode2(menu);
				if (!"1".equals(menu.getEnd())) {
					addChlidNodes2(node);
				}

				childMenuJsons.add(node);
			}
		}
		menuJson.setChildren(childMenuJsons);
	}
	/**
	 * 设置菜单button
	 * @return
	 */
	private void addChlidBtn(TreeNode treeNode) {
		List<TreeNode> childNodes = new ArrayList<TreeNode>();
		for (SysMenuBtn btn : childBtns) {
			if (treeNode.getDataId().equals(btn.getMenuId())) {
				TreeNode node = BtnToNode(btn);
				childNodes.add(node);
			}
		}
		treeNode.setChildren(childNodes);
	}

}
